package com.getpebble.android.notifications;

import android.accessibilityservice.AccessibilityService;
import android.accessibilityservice.AccessibilityServiceInfo;
import android.app.Notification;
import android.os.Build.VERSION;
import android.os.Parcelable;
import android.view.accessibility.AccessibilityEvent;
import com.getpebble.android.common.core.trace.Analytics.MobileAppBehavior;
import com.getpebble.android.common.core.trace.Trace;
import com.getpebble.android.common.core.util.ProcessUtil;
import com.getpebble.android.framework.notification.PblNotificationProcessor;
import com.getpebble.android.notifications.model.PblNotification;
import com.getpebble.android.notifications.model.PblNotification.Source;

public class PblAccessibilityService extends AccessibilityService {
    private static final String TAG = PblAccessibilityService.class.getSimpleName();

    public void onCreate() {
        super.onCreate();
        Trace.info(TAG, "onCreate()");
    }

    public void onAccessibilityEvent(AccessibilityEvent event) {
        if (event == null) {
            try {
                Trace.debug(TAG, "Received null accessibility event");
            } catch (Exception e) {
                Trace.error(TAG, "Error handling accessibility event", e);
                MobileAppBehavior.logNotificationServiceError(TAG, false, e);
            } catch (Throwable t) {
                Trace.error(TAG, "Unrecoverable error occurred handling accessiblity event", t);
                MobileAppBehavior.logNotificationServiceError(TAG, true, t);
            }
        } else if (VERSION.SDK_INT >= 18) {
            Trace.debug(TAG, "Ignoring accessibility service event on 4.3+");
        } else {
            boolean isNotificationEvent;
            if (event.getEventType() == 64 && event.getClassName().equals("android.app.Notification")) {
                isNotificationEvent = true;
            } else {
                isNotificationEvent = false;
            }
            if (isNotificationEvent) {
                Trace.info(TAG, "onAccessibilityEvent (notification)");
                Parcelable parcelable = event.getParcelableData();
                if (parcelable instanceof Notification) {
                    final Notification notification = (Notification) parcelable;
                    if (notification == null) {
                        Trace.error(TAG, "Failed to create PblNotification -- notification object was null");
                        return;
                    }
                    CharSequence packageName = event.getPackageName();
                    if (packageName == null) {
                        Trace.warning(TAG, "getPackageName is null");
                        return;
                    }
                    final String sendingPackage = packageName.toString();
                    ProcessUtil.runWithClearedIdentity(new Runnable() {
                        public void run() {
                            PblNotificationProcessor.processNotification(PblNotification.from(notification, sendingPackage, Source.ACCESSIBILITY));
                        }
                    });
                    return;
                }
                Trace.warning(TAG, "parcelable is not a notification");
            }
        }
    }

    public void onInterrupt() {
        Trace.warning(TAG, "onInterrupt");
    }

    protected void onServiceConnected() {
        super.onServiceConnected();
        Trace.debug(TAG, "onServiceConnected()");
        AccessibilityServiceInfo info = new AccessibilityServiceInfo();
        info.flags = 1;
        info.eventTypes = 64;
        info.feedbackType = 16;
        setServiceInfo(info);
    }
}
